import axios from "axios"
import { getToken } from "./composables/auth"
// http://47.99.241.61

// 定义服务地址
const service = axios.create({
  baseURL: "/api",
})

const projectService = axios.create({
  baseURL: "/projects",
})

const resultsService = axios.create({
  baseURL: "/results",
})

// 请求拦截器
const requestInterceptor = (config) => {
  const token = getToken()
  if (token) {
    config.headers["Authorization"] = token
  }
  return config
}

const responseInterceptor = (response) => response.data

const errorInterceptor = (error) => Promise.reject(error)

// 添加请求和响应拦截器
service.interceptors.request.use(requestInterceptor, errorInterceptor)
service.interceptors.response.use(responseInterceptor, errorInterceptor)

projectService.interceptors.request.use(requestInterceptor, errorInterceptor)
projectService.interceptors.response.use(responseInterceptor, errorInterceptor)

resultsService.interceptors.request.use(requestInterceptor, errorInterceptor)
resultsService.interceptors.response.use(responseInterceptor, errorInterceptor)

export { service, projectService , resultsService}